﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace BlackStar
{
    public class CheckboxContentReader : ContentTypeReader<Checkbox>
    {
        protected override Checkbox Read(ContentReader input, Checkbox existingInstance)
        {
            Checkbox result = new Checkbox();
            result.ReadContent(input);
            return result;
        }
    }

    public class Checkbox : Component
    {
        public event ComponentEvent ValueChanged;

        protected bool myChecked = false;
        [ContentSerializer(Optional = true)]
        public virtual bool Checked
        {
            get { return myChecked; }
            set { SetChecked(value); }
        }

        protected virtual void SetChecked(bool value)
        {
            if (myChecked != value)
            {
                myChecked = value;
                OnValueChanged();
            }
        }


        protected virtual void OnValueChanged()
        {
            if (ValueChanged != null) ValueChanged(this);
        }

        protected internal override void ReadContent(ContentReader input)
        {
            base.ReadContent(input);
            myChecked = input.ReadBoolean();
        }

        public override void Draw()
        {
            if (!Visible) return;

            if (myChecked)
            { gui.theme.Toggle.Draw("CheckTrue", myDrawRectangle, Color); }
            else
            { gui.theme.Toggle.Draw("CheckFalse", myDrawRectangle, Color); }
            DrawComponents();
        }

        public override void DoMouseDown(MouseEventArgs e)
        {
            // should fire the value changed event.
            TakeFocus();
            Checked = !Checked;
            OnMouseDown(); 
        }

        public override void DoKeyDown(KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Keys.Space:
                    // should fire the value changed event.
                    Checked = !Checked;
                    break;
                case Keys.Tab:
                    if (myGui.keyboard.Shift)
                    { DoTabPrev(); }
                    else
                    { DoTabNext(); }
                    break;
            }
            OnKeyDown(e);
        }
    }

    public class RadioButtonContentReader : ContentTypeReader<RadioButton>
    {
        protected override RadioButton Read(ContentReader input, RadioButton existingInstance)
        {
            RadioButton result = new RadioButton();
            result.ReadContent(input);
            return result;
        }
    }

    public class RadioButton : Checkbox
    {
        protected override void SetChecked(bool value)
        {
            if (myChecked != value)
            {
                if (value && Parent != null)
                {
                    foreach (Component c in Parent.Children)
                    {
                        if (c is RadioButton)
                        {
                            if(!(c == this)) ((RadioButton)c).Checked = false;
                        }
                    }
                }
                myChecked = value;
                OnValueChanged();
            }
        }

        public override void Draw()
        {
            if (!Visible) return;

            if (myChecked)
            { gui.theme.Toggle.Draw("RadioTrue", myDrawRectangle, Color); }
            else
            { gui.theme.Toggle.Draw("RadioFalse", myDrawRectangle, Color); }
            DrawComponents();
        }
    }
}
